package com.fancy.learncenter.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AudioBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.RelaseAudioDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.db.RecorderAudioDBManager;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * Created by Hyy on 2017/1/9.
 */

public class FragmentLoactionAudioAdapter extends CommonRecycleViewAdapter<AudioBean> {

    Activity mContext;
    List<AudioBean> itemDatas;

    public FragmentLoactionAudioAdapter(Activity mContext, List<AudioBean> itemDatas) {
        super(mContext, R.layout.fragment_location_auido_item, itemDatas);

        this.mContext = mContext;
        this.itemDatas = itemDatas;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final AudioBean item, final int position) {
        TextView time = customViewHold.getView(R.id.time);
        String str = item.getPlayLink().split("/")[item.getPlayLink().split("/").length - 1];
        time.setText(str.split(" ")[0]);

        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        simpleDraweeView.setImageURI(item.getCoverImg());

        TextView title = customViewHold.getView(R.id.title);
        title.setText(item.getName());
        TextView duration = customViewHold.getView(R.id.duration);
        duration.setText(item.getRDurationFormat());

        LinearLayout delete = customViewHold.getView(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecorderAudioDBManager.getInstance(mContext, Constant.SHOWRECOR_TABLENAME).deleteAudioBook(item.getPlayLink());
                FileUtils.deleteFile(item.getPlayLink());
                itemDatas.remove(position);
                notifyItemChanged(position);
                ToastUtil.show("删除成功");

            }
        });

        LinearLayout updata = customViewHold.getView(R.id.updata);
        updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map map = new HashMap();
                map.put("filePath", item.getPlayLink());
                map.put("showResourceName", item.getName());
                map.put("recordImg", item.getCoverImg());
                map.put("intro", "");
                map.put("type", 3);
                map.put("customerId", LoginUserSharePrefernce.getCustomId());
                HttpMehtod.getInstance().upAudio(map, new CustomNetSubscriber(mContext) {
                    @Override
                    public void onNext(Response<ResponseBody> responseBodyResponse) {
                        //解析数据
                        BaseBean<RelaseAudioDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), RelaseAudioDataBean.class);
                        //数据业务逻辑处理
                        CustomNetCallBack callBack = new CustomNetCallBack(getContext(),baseBean);
                        callBack.setCallBack(new CustomNetCallBack.NetCallBack<RelaseAudioDataBean>() {
                            @Override
                            public void success(RelaseAudioDataBean dataBean) {
                                if (!TextUtils.isEmpty(dataBean.getResult())) {
                                    ToastUtil.show("上传成功");
                                    itemDatas.remove(position);
                                    notifyItemChanged(position);
                                    FileUtils.deleteFile(item.getPlayLink());
                                }
                            }

                            @Override
                            public void fail(String errorCode) {

                            }
                        }).dealData();

                    }
                });

            }
        });
    }
}
